package com.csti.delio.pagos.client.empleados;

import java.util.ArrayList;
import java.util.List;


import com.csti.delio.main.client.idioma.DelioConstants;
import com.csti.delio.main.client.idioma.DelioMessages;
import com.csti.delio.main.client.util.Utilities;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;


public class PanelGridEmpleados extends ContentPanel {
	
    private DelioConstants k=GWT.create(DelioConstants.class);
    private DelioMessages messages=GWT.create(DelioMessages.class);
	private BeanModel selectedModel;
	private List<BeanModel> selectedModels;
	public static Grid<BeanModel> grid;
	private Button btnDelete;
	private Integer selectedSize;
	private ToolBar toolbar = new ToolBar();
	PagingToolBar toolBarPag;
	private ColumnModel cm;

	public PanelGridEmpleados() {
		super();
		setFrame(true);
		setAnimCollapse(false);
		setIcon(IconHelper.createPath("images/icons/table.png"));
		setHeading(k.ListaEmpleados());
		setLayout(new FitLayout());
		

		btnDelete = new Button("Eliminar",IconHelper.createPath("images/icons/delete.gif"));
		btnDelete.setEnabled(false);
		toolbar.add(btnDelete);
		
		toolBarPag = new PagingToolBar( Utilities.itemsPerPage);
		toolBarPag.bind(null);

		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		final CheckBoxSelectionModel<BeanModel> sm = new CheckBoxSelectionModel<BeanModel>();

		columns.add(sm.getColumn());
		columns.add(new ColumnConfig("dni", k.dni(), 100));
		columns.add(new ColumnConfig("nombres", k.Nombres(), 100));
		columns.add(new ColumnConfig("apellidos", k.Apellidos(), 100));
		columns.add(new ColumnConfig("cargo", k.cargo(), 100));
		columns.add(new ColumnConfig("sueldo", k.sueldo(), 100));
		columns.add(new ColumnConfig("fechainicio", k.fechainicio(), 100));
		columns.add(new ColumnConfig("cargo", k.cargo(), 100));
		columns.add(new ColumnConfig("tipocontrato", k.tipocontrato(), 100));
		columns.add(new ColumnConfig("tipoempleo", k.tipoempleo(), 100));
		columns.add(new ColumnConfig("formapago", k.formapago(), 100));
		
	
		cm = new ColumnModel(columns);

		grid = new Grid<BeanModel>(null, cm);
		grid.setAutoExpandColumn("apellidos");
		grid.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {
			@Override
			public void handleEvent(GridEvent<BeanModel> be) {
				//Empleados.loader.load(0, Utilities.itemsPerPage);
			}
		});

		grid.setLoadMask(true);
		grid.setBorders(true);

		final Listener<MessageBoxEvent> l = new Listener<MessageBoxEvent>() {
			@Override
			public void handleEvent(MessageBoxEvent ce) {
				String button_id = ce.getButtonClicked().getItemId();
				if (Dialog.YES == button_id) {
					if (selectedSize == 1) {
						Integer id = selectedModel.get("id");
						//Empleados.delete(id);
					} else if (selectedSize > 1) {
						String ids = Utilities.toIds(selectedModels);
						//Empleados.deleteAll(ids);
					}
				}
			}
		};

		SelectionListener<ButtonEvent> sl_delete;
		sl_delete = new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				String mensaje = "";
				if (selectedSize == 1) {
					String nombres = selectedModel.get("nombres");
					mensaje =  messages.empleadoEstaSeguro(nombres);
				} else if (selectedSize > 1) {
					mensaje = k.eliminarAll();
				}
				MessageBox.confirm(k.confirmacion(), mensaje, l)
						.getDialog().getButtonById(Dialog.YES)
						.setText(k.si());

			}

		};
		btnDelete.addSelectionListener(sl_delete);

		grid.setSelectionModel(sm);
		grid.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<BeanModel>>() {

					@Override
					public void handleEvent(SelectionChangedEvent<BeanModel> be) {
						selectedSize = be.getSelection().size();

						if (selectedSize == 0) {
							
							btnDelete.setEnabled(false);
							btnDelete.setEnabled(false);
												
						} else if (selectedSize == 1) {

							selectedModel = be.getSelectedItem();
						
							btnDelete.setEnabled(true);
							

						} else if (selectedSize > 1) {
							selectedModels = be.getSelection();
						
							btnDelete.setEnabled(false);
						}
					}

				});

		setTopComponent(toolbar);
		setBottomComponent(toolBarPag);

		grid.addPlugin(sm);

		add(grid);

	}

	
}
